<div class="refine-search-by">
        	Refine Search by<%=@tom-%>
                <br>
                <%=render :partial=>"filter_close_options" -%>
        </div>
        <div class="refine-search-options">
          <div class="filter-boxes">
		  	<div class="refine-search-category"><%=@category.name -%></div>
          <%


          @sub_categories=@category.sub_categories-%>
              <% params[:location] =  (params[:location].nil?) ? "" : params[:location].strip
                   if params[:location].empty?
                     location=''
                   else
                     location="@location("+params[:location].to_s+")"
                   end%>
              <%@sub_categories.each do|sub_category|-%>
              <%#search_term = params[:keyword].split(" ").collect{|x| "title like '%#{x}%' or description like '%#{x}%' "}.join(" or ")-%>
<%#=params[:sub_category]%>
                <%#@post_count = Advertisment.count_by_sql("select count(*) from advertisments where "+search_term+" and sub_category_id="+sub_category.id.to_s)%>
                <% if params[:keyword] && !params[:sub_category]%>
                 <%  if params[:university]
                @university=  "@universityname(#{params[:university]})"
                     else
                @university=''
              end
              %>
                             <%#  @post_count = Advertisment.ask_sphinx("@categoryname(#{@category.name}) @cityname(#{@city.name})  #{location} @subcategorystrippedname(\"#{sub_category.stripped_name}\") @title(#{params[:keyword].to_s})|@title(#{params[:keyword].to_s}) | @description(#{params[:keyword].to_s})", :mode => :extended, :limit => 1500)[:total]%>
                             <%  @post_count = Advertisment.ask_sphinx("@categoryname(#{@category.name}) @cityname(#{@city.name})  #{location} #{@university} @subcategorystrippedname(\"#{sub_category.stripped_name}\") @title(#{params[:keyword].to_s}) | @description(#{params[:keyword].to_s})", :mode => :extended, :limit => 1500)[:total]%>



                         <%elsif params[:sub_category] && !params[:keyword]%>
                        <%  @post_count = Advertisment.find_with_sphinx(" #{location} @categoryname(#{@category.name}) #{@university} @subcategorystrippedname(\"#{sub_category.stripped_name}\") @cityname(#{@city.name})", :sphinx => {:mode => :extended, :limit => 1500}).length%>
                        <%elsif params[:sub_category] && params[:keyword]%>
                        <%#  @post_count = Advertisment.ask_sphinx("#{location} @categoryname(#{@category.name}) @title(#{params[:keyword].to_s})|@title(#{params[:keyword].to_s}) | @description(#{params[:keyword].to_s}) @subcategorystrippedname(\"#{sub_category.stripped_name}\") @cityname(#{@city.name})", :mode => :extended, :limit => 1500)[:total]%>

                           <%  @post_count = Advertisment.ask_sphinx("#{location} @categoryname(#{@category.name}) #{@university} @title(#{params[:keyword].to_s}) | @description(#{params[:keyword].to_s}) @subcategorystrippedname(\"#{sub_category.stripped_name}\") @cityname(#{@city.name})", :mode => :extended, :limit => 1500)[:total]%>

                        <%else%>
                        <%  @post_count = Advertisment.ask_sphinx("@cityname(#{@city.name}) @categoryname(#{@category.name}) #{@university} @subcategorystrippedname(\"#{sub_category.stripped_name}\")",  :mode => :extended, :limit => 1500)[:total]%>
                        <%#@post_count=Advertisment.count_by_sql("select count(*) from advertisments where sub_category_id="+sub_category.id.to_s)-%>
                        <%end%>
                <div class="refine-search-filters">
				
                  <%if params[:sub_category]==sub_category.stripped_name%>
                  <font style="font-size:10px;font-weight:bold;color:blue;text-decoration:underline;">
                   <%= sub_category.name+" ("+@post_count.to_s+")" -%></font> &nbsp;
                   <%=link_to image_tag("small_close.jpg",:alt=>"Remove this filter",:border=>"0px"),:search=>"Filter",:university=>params[:university],:keyword=>params[:keyword],:location=>params[:location],:city=>params[:city]%>
                    <%else%>
                    <%=link_to sub_category.name+" ("+@post_count.to_s+")",:search=>"Filter",:keyword=>params[:keyword],:city=>params[:city],:location=>params[:location],:sub_category=>sub_category.stripped_name,:university=>params[:university],:city=>params[:city]-%>
                  <%end -%>
                </div>
               <%end -%>
          </div>
		  <div class="filter-boxes">
          <div class="refine-search-category">Popular Universities</div>

          <%@counter=0%>

          <%@popular.each do |p|  -%>
          <%if @counter<5 %>
           <%#  @post_count = Advertisment.ask_sphinx("@title(#{params[:keyword].to_s})|@title(#{params[:keyword].to_s}) | @description(#{params[:keyword].to_s}) #{location} @categoryname(#{@category.name}) @subcategorystrippedname(\"#{params[:sub_category]}\") @cityname(#{@city.name}) @universityname(\"#{ p[0].to_s}\")", :mode => :extended, :limit => 1500)[:total]-%>
           <%  @post_count = Advertisment.ask_sphinx("@title(#{params[:keyword].to_s}) | @description(#{params[:keyword].to_s}) #{location} @categoryname(#{@category.name}) @subcategorystrippedname(\"#{params[:sub_category]}\") @cityname(#{@city.name}) @universityname(\"#{ p[0].to_s}\")", :mode => :extended, :limit => 1500)[:total]-%>

           <div class="refine-search-filters">
             <%if params[:university]%>

              

                  <%if params[:university]==University.find_by_name(p[0]).name-%>
                    <font style="font-size:10px;font-weight:bold;color:blue;text-decoration:underline;">
                    <%=p[0].to_s+ " ("+ @post_count.to_s+")"%></font>&nbsp;
                    <%=link_to image_tag("small_close.jpg", :alt=>"Remove this filter",:border=>"0px"),:search=>"Filter",:keyword=>params[:keyword],:location=>params[:location],:sub_category=>params[:sub_category],:city=>params[:city]-%>
                    <%else%>
                     <%=link_to p[0].to_s+ " ("+ @post_count.to_s+")",{:university=>University.find_by_name(p[0]).name,:keyword=>params[:keyword], :search=>"Filter",:sub_category=>params[:sub_category],:city=>params[:city]}-%>
                  <%end -%>
                  
             <%else%>
              

    
                  <%if params[:university]==University.find_by_name(p[0]).name-%>
                     <%=p[0].to_s+ " ("+ @post_count.to_s+")"%></font>&nbsp;
                     <%=link_to image_tag("small_close.jpg", :border=>"0px"),:search=>"Filter",:keyword=>params[:keyword],:location=>params[:location],:city=>params[:city]-%>
                    <%else%>
                   <%=link_to p[0].to_s+ " ("+ @post_count.to_s+")",{:university=>University.find_by_name(p[0]).name, :search=>"Filter",:keyword=>params[:keyword],:city=>params[:city]}-%>
                  <%end -%>
     <%end%>
           </div>
          <%end -%>
         <% @counter=@counter+1%>
          <%end -%>

	</div>


			<div class="filter-boxes">
             <%@universities.each do |university| -%>
                <%AdvertismentAttributeFieldValue.generate_list(university.id,@category.id).each do |field_item, options| -%>
			         <%@array=Category.find_by_stripped_name(params[:category]).form_labels-%>
                        <%@array.each do |a|%>
                          <%if a==(field_item)%>
                              <div class="refine-search-category">
                            <%=field_item.humanize -%>
                              </div>
                              <%options.each do |option_item,option_count| -%>
                                <%if option_item.class == Array -%>
                                  <%="#{option_item.inspect}" -%>
                                <%else%>
                                  <%option_count["advertisments"].each do |advertisment_id|%>
                                      <%@sub_category_id=Advertisment.find(advertisment_id).sub_category_id-%>
                                  <%end -%>
                                  <div class="refine-search-filters">
                                  <%=link_to "#{option_item.humanize} (#{option_count["count"]})", {:controller=>"listings", :action => "filtered_advertisments",:category=>params[:category],:university=>university.stripped_name, :search=>"True",:advertisments => option_count["advertisments"].compact.join("-"),:search_subcat=>@sub_category_id } -%>
                                  </div>
                                <%end -%>
                          <%end -%>
                        <%end -%>
                <%end -%>
            <%end -%>
         <%end -%>
       	</div>
        </div>